#include "UpdateChannelCountForm.h"
#include "ui_UpdateChannelCountForm.h"
#include "toObject/ModelXml.h"

UpdateChannelCountForm::UpdateChannelCountForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UpdateChannelCountForm)
{
    ui->setupUi(this);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);//选中单行
}

UpdateChannelCountForm::~UpdateChannelCountForm()
{
    delete ui;
}

void UpdateChannelCountForm::InitUiData()
{
    ui->tableWidget->clear();
    QStringList arrayHead = {"转发通道ID","转发通道名称","转发通道描述","转发协议","接口类型"};
    ui->tableWidget->setColumnCount(arrayHead.size());
    ui->tableWidget->setHorizontalHeaderLabels(arrayHead);          //表头


    std::map<int,stUpdateChannel> channels = ModelXml::GetInstance()->GetUpdateChannelList();
    ui->tableWidget->setRowCount(channels.size());
    std::map<int,stUpdateChannel>::iterator it = channels.begin();
    for(int row = 0 ; it != channels.end() ; it++,row++)
    {
        QTableWidgetItem * item = new QTableWidgetItem(QString::number(it->second.id));
        ui->tableWidget->setItem(row,0, item);
        QTableWidgetItem * item1 = new QTableWidgetItem(it->second.name.c_str());
        ui->tableWidget->setItem(row,1, item1);
        QTableWidgetItem * item2 = new QTableWidgetItem(it->second.desc.c_str());
        ui->tableWidget->setItem(row,2, item2);
        QTableWidgetItem * item3 = new QTableWidgetItem(it->second.protoName.c_str());
        ui->tableWidget->setItem(row,3, item3);

        QTableWidgetItem * item4 = nullptr;
        if(it->second.intarfaceType == stUpdateChannel::Intarface_Comm)
        {
            item4 = new QTableWidgetItem("串口");
        }
        else if(it->second.intarfaceType == stUpdateChannel::Intarface_TcpClinet)
        {
            item4 = new QTableWidgetItem("TCP客户端");
        }
        else if(it->second.intarfaceType == stUpdateChannel::Intarface_TcpServer)
        {
            item4 = new QTableWidgetItem("TCP服务端");
        }
        else if(it->second.intarfaceType == stUpdateChannel::Intarface_Udp)
        {
            item4 = new QTableWidgetItem("UDP");
        }
        ui->tableWidget->setItem(row,4, item4);
    }
}
